﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using AppToolkit.WPF.Properties;

namespace AppToolkit.WPF.UI.Search
{
    /// <summary>
    /// Helps resolving the returned error to a user readable error message.
    /// A default set of resource strings is provided by base implementation. Strings can be overridden by specifying a 
    /// different <see cref="ResourcePrefix"/>. These results in the converter to query for a resource id consisting of
    /// <see cref="ResourcePrefix"/> + <see cref="ErrorInfo"/> name.
    /// </summary>
    /// <example>
    /// ResourcePrefix = 'Prefix'  =>  resource id should look like 'PrefixResultSetIsEmpty', ...
    /// </example>
    public class ErrorInfoConverter : IValueConverter
    {
        private const string defaultPrefix = "UI_Search_ErrorInfo_";
        private global::System.Resources.ResourceManager _resourceMan;

        /// <summary>
        /// ctor
        /// </summary>
        public ErrorInfoConverter()
        {
            this.ResourcePrefix = defaultPrefix;
        }

        /// <summary>
        /// Set the ResourceManager to resolve resource ids from a different assembly.
        /// </summary>
        public global::System.Resources.ResourceManager ResourceManager
        {
            set { _resourceMan = value; }
            get { return _resourceMan; }
        }

        /// <summary>
        /// Overrides the default ResourcePrefix to be used to generate the resource id.
        /// If no override is set, it returns the default ResourcePrefix.
        /// </summary>
        public string ResourcePrefix { get; set; }

        #region IValueConverter Members
        /// <summary>
        /// Resolves ErrorInfo to a user displayable string.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is ErrorInfo)
            {
                if ((ErrorInfo)value == ErrorInfo.NoError)
                    return DependencyProperty.UnsetValue;

                string postfix = Enum.GetName(typeof(ErrorInfo), value);

                var rscManager = (this.ResourceManager != null) ? this.ResourceManager : Resources.ResourceManager;

                return rscManager.GetString(this.ResourcePrefix + postfix);
            }
            return DependencyProperty.UnsetValue;
        }

        /// <summary>
        /// Not implemented!
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
